package b649a01.tomasulo;

import b649a01.runners.sim_t;

/***
 * 
 * Class representing an instruction selected to be executed.
 * 
 * @author Chao Sun
 * @version 1.0
 * 
 */
public class to_exe {
	private sim_t simulator;
	private int robEntry;
	private String operator;
	private Double vj, vk;
	private Double value;
	private int cycleNum;
	private boolean busy;

	public to_exe(sim_t s, int re, String op, Double j, Double k, int c) {
		simulator = s;
		robEntry = re;
		operator = op;
		vj = j;
		vk = k;
		cycleNum = c;
		busy = true;
	}

	public boolean execute() {
		if (busy == true) {
			if (operator.equals("ADD") || operator.equals("ADD.D")) {
				value = vj.doubleValue() + vk.doubleValue();
			} else if (operator.equals("SUB") || operator.equals("SUB.D")) {
				value = vj.doubleValue() - vk.doubleValue();
			} else if (operator.equals("MUL") || operator.equals("MUL.D")) {
				value = vj.doubleValue() * vk.doubleValue();
			} else if (operator.equals("DIV")) {
				value = (double) (int) (vj.intValue() / vk.intValue());
			} else if (operator.equals("DIV.D")) {
				value = vj.doubleValue() / vk.doubleValue();
			} else if (operator.equals("L") || operator.equals("LI")
					|| operator.equals("L.D") || operator.equals("S")
					|| operator.equals("S.D") || operator.equals("MOVE")
					|| operator.equals("MOVE.D")) {
				value = vj;
			} else if (operator.equals("BEQ")) {
				if (vj.intValue() == vk.intValue()) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			} else if (operator.equals("BNEQ")) {
				if (vj.intValue() != vk.intValue()) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			} else if (operator.equals("BGTZ")) {
				if (vj.intValue() > 0) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			} else if (operator.equals("BLTZ")) {
				if (vj.intValue() < 0) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			} else if (operator.equals("BEQ.D")) {
				if (vj.doubleValue() == vk.doubleValue()) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			} else if (operator.equals("BNEQ.D")) {
				if (vj.doubleValue() != vk.doubleValue()) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			} else if (operator.equals("BGTZ.D")) {
				if (vj.doubleValue() > 0) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			} else if (operator.equals("BLTZ.D")) {
				if (vj.doubleValue() < 0) {
					value = new Double(1);
				} else {
					simulator.wrongPredict();
				}
			}
			busy = false;
		}
		cycleNum--;

		if (cycleNum <= 0) {
			return true;
		} else {
			return false;
		}
	}

	public int getROBEntry() {
		return robEntry;
	}

	public String getOperator() {
		return operator;
	}

	public Double getValue() {
		return value;
	}

	public int getCycleNum() {
		return cycleNum;
	}

}